package com.btcode.web.core.unit;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * 地址处理工具类
 *
 * @author gislin
 */
public class URLUtil {

    private static String projectName;

    public static String getProjectName() {
        return projectName;
    }

    public static void setProjectName(String projectName) {

        if (projectName.startsWith("/")) {
            projectName = projectName.replaceFirst("/", "");
        }

        URLUtil.projectName = projectName;
    }

    public static String getURLWithoutProjectName(ServletRequest request) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURI();
        uri = uri.replaceFirst("/" + projectName, "");
        return uri;
    }

    public static String getURL(ServletRequest request) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURL().toString();
        return uri;
    }

    public static String prepareURL(String url) {
        if (!url.startsWith("/")) {
            url = "/" + url;
        }
        return url;
    }

}
